<template>
  <el-dialog
    :before-close="_close"
    :visible.sync="propValue"
    title="选择用户"
    width="30%"
    destroy-on-close
  >
    <el-tree
      ref="menu"
      :data="treeData"
      :default-checked-keys="permissions"
      :props="{label:'username'}"
      default-expand-all
      empty-text="暂无配置"
      node-key="id"
      show-checkbox
    ></el-tree>
    <span slot="footer" class="dialog-footer">
            <el-button size="medium" type="primary" @click="onSubmit">保存</el-button>
            <el-button size="medium" @click="_close">取消</el-button>
        </span>
  </el-dialog>
</template>
<script>

import {getRoleIdByUserId, userAddRole} from "@/api/userRole";
import {findAllNoPage} from "@/api/role";
import {getSubUserIdByUserId, userAddSubUser} from "@/api/userSubuser";

export default {
  name: 'rolePopup',
  components: {},
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    currentData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      activeName: '1',
      treeData: [],
      permissions: [],
      checkStrictly: true,
    }
  },
  computed: {
    propValue: {
      get() {
        return this.value
      },
      set(nval) {
        this.$emit('input', nval)
      },
    },
  },
  methods: {
    async getTree() {
      const params = {
        userId: this.currentData.id,
      }
      let {code, data} = await this.$api.user.findAllNoPage(params)
      if (code === 200) {
        await this.getRolePermissions(data)
      }
    },

    async getRolePermissions(treeData) {
      const params = {
        userId: this.currentData.id,
      }
      let {code, data} = await this.$api.userSubuser.getSubUserIdByUserId(params)
      if (code === 200) {
        this.permissions = data
        this.treeData = treeData
      }
    },
    async onSubmit() {
      let params = {
        userId: this.currentData.id,
        userIds: this.$refs.menu.getCheckedKeys().concat(this.$refs.menu.getHalfCheckedKeys()),
      }

      let {code, msg} = await this.$api.userSubuser.userAddSubUser(params)
      if (code === 200) {
        this.$message.success('配置成功' || msg)
        this.$emit('input', false)
        this.$emit('success')
      }
    },
    //关闭弹窗
    _close() {
      this.treeData = []
      this.permissions = []
      this.$emit('input', false)
    },
  },
}
</script>
<style lang="scss" scoped>

</style>
